home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 352 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  2. Message-ID: <4fl2cn$hue@mulga.cs.mu.OZ.AU>
  3. X-Original-Date: 11 Feb 1996 15:35:50 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 12 Feb 96 05:03:19 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Observations on templates
  9. Organization: Comp Sci, University of Melbourne
  10. References: <ACVI83na99@qsar.chem.msu.su> <4fa6d0$115g@news.gate.net>
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMR7KOuEDnX0m9pzZAQGoWgGAlZHAIcIBefVaSTeQhykePENU9Aa4a4cf
  13.     Jvqfc2rAkls7rKu02UJyRAN7g+or4wHb
  14.     =ILLW
  15.  
  16. solution@gate.net (Ken Walter) writes:
  17.  
  18. >I always thought the following was interesting:
  19. >
  20. >template <class T, class S> T cast_d( S s) { return dynamic_cast<T>s; }
  21.  
  22. That won't quite do what you want, because you are using
  23. pass-by-value.  That will result in copying, giving `cast_d' quite
  24. different semantics to `dynamic_cast'.  You should use
  25. pass-by-reference instead.  You then need to overload the function to
  26. allow all possible sorts of references:
  27.  
  28. template <class T, class S> T cast_d(S &s)      { return dynamic_cast<T>s; }
  29. template <class T, class S> T cast_d(const S &s) { return dynamic_cast<T>s; }
  30. template <class T, class S> T cast_d(volatile S &s)
  31.                          { return dynamic_cast<T>s; }
  32. template <class T, class S> T cast_d(const volatile S &s)
  33.                          { return dynamic_cast<T>s; }
  34.  
  35. --
  36. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  37. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  38. ---
  39. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  40.   Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  41.   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  42.